<template>
	<view class="pageBg">
		<Head ref="Head" title="搜索详情" @handleClear="handleClearIpt" @handleSearch="handleIptSearch" type="searchShow"
			:isBack="false"></Head>
		<!-- #ifdef MP-TOUTIAO || MP-ALIPAY -->
		<view class="search">
			<u-search height="70rpx" v-model="keyword" clearSize="40" :clearStyle="{padding:'6rpx 0 0'}"
				:input-style="{color:'#838383',fontSize:'14px',padding:'8rpx 0'}" bg-color="#fff" :clearabled="true"
				:show-action="false" @clear="handleClearIpt"></u-search>
		</view>
		<!-- #endif -->
		<view class="jobList" v-show="type == 'job'">
			<ListFilters @doSearchByDistrict="doSearchByDistrictCom" @doSearchByOther="doSearchByOtherCom"
				ref="jobFilters" :params="typeParams" :otherFilterType="otherFilterType">
				<view class="tabBtns">
					<view :class="filterType == 'filterJob' ? 'btn ac' : 'btn'" @click="handleTabClick('filterJob')">职位
					</view>
					<view :class="filterType == 'filterCompany' ? 'btn ac' : 'btn'"
						@click="handleTabClick('filterCompany')">公司</view>
				</view>
			</ListFilters>
			<view v-if="filterType == 'filterJob'">
				<JobItem v-for="(item,index) in dataset" :key="item.id" :item="item" @toDetail="toJobShowDetail">
				</JobItem>
			</view>
			<view v-if="filterType == 'filterCompany'">
				<CompanyItem v-for="(item,index) in dataset" :key="item.id" :item="item" @toDetail="toCompanyDetail">
				</CompanyItem>
			</view>
			<u-loadmore v-if="dataset&&dataset.length >= pagesize" class="u-loadmore"
				:status="finished_status"></u-loadmore>
			<Empty v-if="show_empty"></Empty>
		</view>
		<view class="companyList" v-show="type == 'company'">
			<ListFilters ref="jobFilters" :params="companyParams" otherFilterType="companyListType"
				@doSearchByDistrict="doSearchByDistrictCom" @doSearchByOther="doSearchByOtherCom">
				<view class="tabBtns">
					<view class="btn ac">企业 </view>
				</view>
			</ListFilters>
			<CompanyItem v-for="(item,index) in dataset" :key="item.id" :item="item" @toDetail="toCompanyDetail">
			</CompanyItem>
			<u-loadmore v-if="dataset&&dataset.length >= pagesize" class="u-loadmore"
				:status="finished_status"></u-loadmore>
			<Empty v-if="show_empty"></Empty>
		</view>
		<view class="resumeList" v-show="type == 'resume'">
			<ListFilters ref="jobFilters" :params="resumeParams" otherFilterType="resumeListType"
				@doSearchByDistrict="doSearchByDistrictCom" @doSearchByOther="doSearchByOtherCom">
				<view class="tabBtns">
					<view class="btn ac">人才</view>
				</view>
			</ListFilters>
			<ResumeItem v-for="(item,index) in dataset" :key="item.id" :item="item" @toDetail="toResumeShowDetail">
			</ResumeItem>
			<u-loadmore v-if="dataset&&dataset.length >= pagesize" class="u-loadmore"
				:status="finished_status"></u-loadmore>
			<Empty v-if="show_empty"></Empty>
		</view>

	</view>
</template>

<script>
	import http from '@/utils/http'
	import api from '@/api'
	import JobItem from '@/components/listItem/jobItem.vue'
	import ResumeItem from '@/components/listItem/resumeItem.vue'
	import CompanyItem from '@/components/listItem/companyItem.vue'
	import ListFilters from '@/components/ListFilters.vue'
	export default {
		components: {
			JobItem,
			ResumeItem,
			CompanyItem,
			ListFilters
		},
		data() {
			return {
				filterType: 'filterJob',
				otherFilterType: 'jobListType',
				show_empty: false,
				keyword: '',
				history_key: '',
				history: [],
				type: '',
				page: 1,
				pagesize: 15,
				jobParams: {
					search_type: 'list',
					keyword: '',
					district1: '',
					district2: '',
					district3: '',
					districtName: '地区',
					category1: '',
					category2: '',
					category3: '',
					categoryName: '职位',
					experience: '',
					minwage: '',
					maxwage: '',
					filter_apply: '',
					nature: '',
					education: '',
					tag: '',
					settr: ''
				},
				companyParams: {
					keyword: '',
					district1: '',
					district2: '',
					district3: '',
					districtName: '地区',
					trade: '',
					scale: '',
					nature: ''
				},
				resumeParams: {
					keyword: '',
					district1: '',
					district2: '',
					district3: '',
					districtName: '地区',
					experience: '',
					education: '',
					sex: '',
					minage: '',
					maxage: '',
					major: '',
					minwage: '',
					maxwage: '',
					tag: '',
					settr: ''
				},
				params: {},
				dataset: [],
				finished_status: 'loadmore',
				typeParams: {},
				selectArrOnload: [],
				selectResumeArrOnload: []
			};
		},
		onLoad(options) {
			if (options.paramsCurrent && options.paramsCurrent != '') {
				// 分享查看页面
				let params = JSON.parse(options.paramsCurrent)
				this.params = params
				this.type = params.type
				this.filterType = params.filterType
				params.type == 'job' ? this.jobParams = params : params.type == 'company' ? this.companyParams = params :
					this
					.resumeParams = params
				this.$nextTick(() => {
					if (params.type == 'job') {
						this.$refs.jobFilters.initShareData(this.jobParams)
					} else if (params.type == 'company') {
						this.$refs.jobFilters.initShareData(this.companyParams)
					} else {
						this.$refs.jobFilters.initShareData(this.resumeParams)
					}
				})
				this.$refs.Head.setKeyword(params.keyword)
				this.init(true)
			}
			console.log(options,'options---searchShow')
			// #ifdef MP-WEIXIN
			if (options.keyword) {
				this.$refs.Head.setKeyword(decodeURIComponent(options.keyword))
			}
			// #endif
			if (options.keyword) {
				this.keyword = decodeURIComponent(options.keyword)
			}
			if (options.history_key) {
				this.history_key = options.history_key
			}
			console.log(this.history_key,'onload--------------')
			if (options.type) {
				this.type = options.type
			}
			this.init(true)
		},
		onShareAppMessage() {
			let type = this.type
			let params = type == 'job' ? this.jobParams : type == 'company' ? this.companyParams : this.resumeParams
			params.type = type
			params.filterType = this.filterType
			return {
				path: '/commonViews/SearchShow?paramsCurrent=' + JSON.stringify(params),
				title: params.keyword + '-' + this.$store.state.config.sitename
			}
		},
		onShareTimeline() {
			let params = type == 'job' ? this.jobParams : type == 'company' ? this.companyParams : this.resumeParams
			params.type = type
			params.filterType = this.filterType
			return {
				path: '/commonViews/SearchShow?paramsCurrent=' + JSON.stringify(params),
				title: params.keyword + '-' + this.$store.state.config.sitename
			}
		},
		onReachBottom() {
			this.finished_status = 'loading';
			this.page++
			this.init(false)
		},
		onPageScroll(res) {
			uni.$emit('onPageScroll', res.scrollTop)
		},
		methods: {
			// 文本框搜索
			handleIptSearch(keyword) {
				var val = keyword || this.keyword
				this.keyword = val
				this.updateHistory(this.keyword)
				// #ifdef MP-WEIXIN
				if (this.keyword) {
					this.$refs.Head.setKeyword(this.keyword)
				}
				// #endif
				this.init(true)
			},
			// 搜索记录
			updateHistory(keyword) {
				if (keyword === '') {
					return
				}
				let arr = []
				let list = uni.getStorageSync(this.history_key)
				arr = list != '' ? JSON.parse(list) : null
				arr = arr === null ? [] : arr
				arr.unshift(keyword)

				let uniqueSet = new Set(arr)
				let historyArr = [...uniqueSet]
				if (historyArr.length > 15) {
					historyArr.pop()
				}
				console.log(this.history_key,'updateHistory--------------')
				uni.setStorageSync(this.history_key, JSON.stringify(historyArr))
				this.history = historyArr
			},
			toCompanyDetail(id) {
				this.dataset.forEach(item => {
					if (item.id == id) {
						item.clicked = true
					}
				})
				let selectComArr = this.selectComArrOnload == undefined ? [] : this.selectComArrOnload
				if (selectComArr.indexOf(id) == -1 || selectComArr.length == 0) {
					selectComArr.push(id)
				}
				let selectComArrStr = JSON.stringify(selectComArr)
				uni.setStorageSync('selectComArr', selectComArrStr)
				uni.navigateTo({
					url: '/commonViews/CompanyShow?id=' + id
				})
			},
			getColorChange() {
				let LoginOrNotLocal = uni.getStorageSync('LoginOrNot');
				let LoginOrNot = this.$store.state.LoginOrNot.toString();
				uni.setStorageSync('LoginOrNot', LoginOrNot); //更新缓存中登录状态
				if (LoginOrNot != LoginOrNotLocal) { //登录状态改变时清空缓存数据
					uni.setStorageSync('selectArr', null); //职位
					uni.setStorageSync('selectResumeArr', null); //简历 
					uni.setStorageSync('selectComArr', null); //企业
				}
				this.selectResumeArrOnload = uni.getStorageSync('selectResumeArr') == null || uni.getStorageSync(
					'selectResumeArr') == '' || uni.getStorageSync(
					'selectResumeArr') == undefined ? [] : JSON.parse(uni.getStorageSync('selectResumeArr'))
				this.selectResumeArrOnload.forEach(items => {
					this.dataset.forEach(item => {
						if (item.id == items) {
							item.clicked = true
						}
					})
				})
			},
			toJobShowDetail(id) {
				let selectArr = uni.getStorageSync('selectArr')
				selectArr = selectArr == 'null' || selectArr == null || selectArr == '' ? [] : JSON.parse(selectArr)
				if (selectArr.indexOf(id) == -1 || selectArr.length == 0) {
					selectArr.push(id)
				}
				this.selectArrOnload = selectArr
				uni.setStorageSync('selectArr', JSON.stringify(selectArr))
				for (let i in this.dataset) {
					if (this.dataset[i].id == id) {
						this.dataset[i].clicked = true
					}
				}
				uni.navigateTo({
					url: '/commonViews/JobShow?id=' + id
				})
			},
			toResumeShowDetail(id) {
				let selectResumeArr = this.selectResumeArrOnload == undefined ? [] : this.selectResumeArrOnload
				if (selectResumeArr.indexOf(id) == -1) {
					selectResumeArr.push(id)
				}
				let selectResumeArrStr = JSON.stringify(selectResumeArr)
				uni.setStorageSync('selectResumeArr', selectResumeArrStr)
				for (let i in this.dataset) {
					if (this.dataset[i].id == id && !this.dataset[i].clicked) {
						this.dataset[i].clicked = true
					}
				}
				uni.navigateTo({
					url: '/commonViews/ResumeShow?id=' + id
				})
			},
			init(init) {
				switch (this.type) {
					case 'job':
						this.getJobList(init)
						break;
					case 'resume':
						this.getResumeList(init)
						break;
					case 'company':
						this.getCompanyList(init)
						break;
					default:
						break;
				}
			},
			getJobList(init) {
				if (this.params.keyword && this.params.keyword != '') {
					this.jobParams.keyword = this.params.keyword
				} else {
					this.jobParams.keyword = this.keyword
				}
				this.show_empty = false
				let conditions = {
					...this.jobParams
				}
				if (init === true) {
					this.page = 1
					this.finished_status = 'loading'
				}
				conditions.page = this.page
				conditions.pagesize = this.pagesize
				http
					.get(api.joblist, conditions)
					.then(res => {
						if (init === true) {
							this.dataset = [...res.data.data.items]
							this.showLayer = parseInt(res.data.show_mask) === 1
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}

						// 判断用户是否点击查看过某些数据
						if (this.selectArrOnload && this.selectArrOnload.length > 0) {
							for (let j in this.selectArrOnload) {
								for (let i in this.dataset) {
									if (this.dataset[i].id == this.selectArrOnload[j]) {
										this.dataset[i].clicked = true
									} else {
										this.dataset[i].clicked = false
									}
								}
							}
						} else {
							for (let i in this.dataset) {
								this.dataset[i].clicked = false
							}
						}
						this.finished_status = 'loadmore';
						// // 数据全部加载完成
						if (res.data.data.items && res.data.data.items.length < this.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items && res.data.data.items.length === 0) {
								this.show_empty = true
								this.finished_status = 'nomore'
							}
						}
					})
					.catch(() => {})
			},
			getCompanyList(init) {
				if (this.params.keyword && this.params.keyword != '') {
					this.companyParams.keyword = this.params.keyword
				} else {
					this.companyParams.keyword = this.keyword
				}
				this.show_empty = false
				let conditions = {
					...this.companyParams
				}
				if (init === true) {
					this.page = 1
					this.finished_status = 'loading'
				}
				conditions.page = this.page
				conditions.pagesize = this.pagesize
				http
					.get(api.companylist, conditions)
					.then(res => {
						if (init === true) {
							this.dataset = [...res.data.data.items]
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}
						this.dataset.forEach(item => {
							item.clicked = false
						})
						// 数据全部加载完成
						if (res.data.data.items && res.data.data.items.length < this.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items && res.data.data.items.length == 0) {
								this.finished_status = 'nomore'
								this.show_empty = true
							}
						}
					})
					.catch(() => {})
			},
			getResumeList(init) {
				if (this.params.keyword && this.params.keyword != '') {
					this.resumeParams.keyword = this.params.keyword
				} else {
					this.resumeParams.keyword = this.keyword
				}
				this.show_empty = false
				let conditions = {
					...this.resumeParams
				}
				if (init === true) {
					this.page = 1
					this.finished_status = 'loading'
				}
				conditions.page = this.page
				conditions.pagesize = this.pagesize
				http
					.get(api.resumelist, conditions)
					.then(res => {
						if (init === true) {
							this.dataset = [...res.data.data.items]
						} else {
							this.dataset = this.dataset.concat(res.data.data.items)
						}
						this.dataset.forEach(item => {
							item.clicked = false
						})
						this.getColorChange(); //缓存数据标题是否变色
						// 数据全部加载完成
						if (res.data.data.items && res.data.data.items.length < this.pagesize) {
							this.finished_status = 'nomore'
							if (init === false) {
								this.finished_status = 'nomore'
							} else if (res.data.data.items && res.data.data.items.length === 0) {
								this.finished_status = 'nomore'
								this.show_empty = true
							}
						}
					})
					.catch(() => {})
			},
			// 搜索框清空返回
			handleClearIpt() {
				uni.navigateBack()
			},
			// 职位公司切换
			handleTabClick(type) {
				this.page = 1
				this.dataset = []
				this.filterType = type
				this.clearParams()
				this.typeParams = this[this.paramsText()]
				if (type == 'filterJob') {
					this.otherFilterType = 'jobListType'
					this.getJobList(true)
				} else if (type == 'filterCompany') {
					this.otherFilterType = 'companyListType'
					this.getCompanyList(true)
				}
				setTimeout(() => {
					this.$refs.jobFilters.initData()
				}, 100)
			},
			// 地区搜索
			doSearchByDistrictCom(data) {
				if (this.type == 'job') {
					this.jobParams.district1 = data.district1
					this.jobParams.district2 = data.district2
					this.jobParams.district3 = data.district3
					this.jobParams.districtName = data.districtName
				} else if (this.type == 'company') {
					this.companyParams.district1 = data.district1
					this.companyParams.district2 = data.district2
					this.companyParams.district3 = data.district3
					this.companyParams.districtName = data.districtName
				} else if (this.type == 'resume') {
					this.resumeParams.district1 = data.district1
					this.resumeParams.district2 = data.district2
					this.resumeParams.district3 = data.district3
					this.resumeParams.districtName = data.districtName
				}
				this.doSearch({
					district1: data.district1,
					district2: data.district2,
					district3: data.district3
				})
			},
			// 其他搜索
			doSearchByOtherCom(data) {
				this.doSearch(data)
			},
			paramsText() {
				var params = ''
				if (this.type == 'job') {
					if (this.filterType == 'filterJob') {
						params = 'jobParams'
					} else if (this.filterType == 'filterCompany') {
						params = 'companyParams'
					}
				} else {
					params = this.type + 'Params'
				}
				return params
			},
			clearParams() {
				var params = this.paramsText()
				for (const key in this[params]) {
					this[params][key] = ''
				}
			},
			// 搜索通用函数
			doSearch(data) {
				var params = this.paramsText()
				for (const key in data) {
					if (this[params].hasOwnProperty(key)) {
						this[params][key] = data[key]
					}
				}
				this[params].otherNum = data.otherNum
				if (this[params].otherNum == 0) {

				}
				this.page = 1
				this.init(true)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.jobList,
	.companyList,
	.resumeList {
		padding: 0 30rpx;
	}

	.search {
		padding: 27rpx 30rpx 0;
	}

	::v-deep .filter {
		padding-left: 0 !important;
		padding-right: 0 !important;
	}
</style>